using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using PropertyExpression.ControllableQuery;
using PropertyExpression.ControllableQuery.Test;
using PropertyExpression.ControllableQuery.TypedReader;

namespace ConsoleApplication1
{
    public static class QExecutor
    {
        public static IEnumerable<TRecordset> MaterializeReader<TParams, TRecordset>(
            Func<SqlCommand, TParams> paramsFunc, Func<TParams, IQuery<TRecordset>> recordsetFunc)
        {
            using (var sqlCommand = new SqlCommand())
            {
                var query = recordsetFunc(paramsFunc(sqlCommand));
                sqlCommand.CommandText = query.TransformText();
                using (var connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();
                    sqlCommand.Connection = connection;
                    using (var sqlDataReader = sqlCommand.ExecuteReader())
                        while (sqlDataReader.Read())
                            yield return query.GetReader(sqlDataReader).Materialize();
                }
            }
        }

        public static string ConnectionString
        {
            get { return @"Data Source=(local);Initial Catalog=ControllableQuery;Integrated Security=True"; }
        }
    }
}